package org.elec.mac.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by wangtao on 2018/6/23.
 */

public class AESUtil {

    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "AES";
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS5Padding";
    /**
     * 生成key
     */
    private static SecretKeySpec key = new SecretKeySpec(MD5.MD5Encode("asgtqwu28snay79smai8ym0761gamj78").toLowerCase().getBytes(), ALGORITHM);

    /**
     * AES加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptData(String data) throws Exception {
        // 创建密码器
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        // 初始化
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64Util.encode(cipher.doFinal(data.getBytes()));
    }

    /**
     * AES解密
     *
     * @param base64Data
     * @return
     * @throws Exception
     */
    public static String decryptData(String base64Data) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(Base64Util.decode(base64Data)));
    }

    public static void main(String[] args) throws Exception {
        //解密
        String req_info = "hSxZP0F46DwBDXuLnozMKhBxBZz3f2FTaCt81CCkJslf6VdfhW5PQTppEhwHuZUjbcJ0LwUcz6ZNrp7Sdf+uZhXSHvTWAdWrjlQC9P+JtGu8ezSSL4GK0B8I0j8O9u3bAce1aL6J5H0f6VZtMRjqOLi0vqVy+JdqBEieVv16zv0gSa/kgKm4WcvzcBnRizbc0d6nvvqkDFWgeI1yJ8bumRM115pgIPhXoJuSzri1D810rW7Fzoeq8CB55Z8zwjr84+XD0aSuNkhnlobfugUciYBn4BYRWH0IgPOF9jJM2Rn5ivm0wr8XjWjB6lxtFVrUjVTi1jbIsQ2g0cSJeAWxx75zaP6BhPcAabcUbxuUcSDBpVNVipvmuC6uK1OYonRWABrjxbBDCHhFnXkXb9R70kocdLn2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